import smbus
import time

class LedCtl:
    def __init__(self, i2c_bus=1, addr=0x38, reg=0):
        self.bus = smbus.SMBus(i2c_bus)
        self.addr = addr
        self.reg = reg
        self.value = 0

    def read(self):
        """read op will put the IOs of the chip into input mode"""

        data = self.bus.read_i2c_block_data(self.addr, self.reg, 1)
        return data[0]

    def clear(self):
        self.read()
        self.value = 0

    def write(self, data):
        self.bus.write_byte_data(self.addr, self.reg, data)

    def toggle(self, bit, on=True):
        if bit < 0 or bit > 7:
            return
        if on:
            self.value |= (1 << bit)
        else:
            self.value &= ~(1 << bit)
        self.write(self.value)

    def any(self):
        return self.value != 0

    def ifon(self, bit):
        return (self.value & (1 << bit)) != 0

if __name__ == '__main__':
    ledctl = LedCtl()
    ledctl.toggle(0)
    time.sleep(1)
    ledctl.toggle(1)
    time.sleep(1)
    ledctl.toggle(2)
    time.sleep(1)
    ledctl.toggle(3)
    time.sleep(1)
    ledctl.toggle(4)
    time.sleep(1)
    ledctl.toggle(5)
    time.sleep(1)
    print(ledctl.any())
    ledctl.clear()
    print(ledctl.any())
